package com.cn.qtms.util.sms;//类注释设置模板

import com.alibaba.fastjson.JSON;
import com.cn.qtms.dto.SmsMessage;
import com.cn.qtms.util.sms.aliyun.AliyunSmsUtil;
import com.cn.qtms.util.sms.submail.SubmailSmsUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * @Name:SmsMessageListener
 * @Description:
 * @Author: zhangyang
 * @Date: 2019-12-01 13:12
 **/
@Slf4j
@Component
public class SmsMessageListener {
    @Autowired
    private Environment env;

    @Autowired
    private SubmailSmsUtil submailSmsUtil;

    @Autowired
    private AliyunSmsUtil aliyunSmsUtil;

    private static final String ALI_YUN="aliyun";
    private static final String SUB_MAIL="submail";
    /**
     * 发送短信
     */
    @RabbitHandler
    @RabbitListener(queues = "jixiang.sms",containerFactory="rabbitListenerContainerFactory")
    public void sendSms(SmsMessage smsMessage){
        try {
            String mode= env.getProperty("sms.mode");
            HashMap<Object, Object> smsMap = new HashMap<>();
            smsMap.put("code", smsMessage.getCode());
            smsMap.put("time", 5);
            if(ALI_YUN.equalsIgnoreCase(mode)){
                //阿里云短信功能 使用的公司配置
                aliyunSmsUtil.sendSms(smsMessage.getPhone(), JSON.toJSONString(smsMap));
            } else if(SUB_MAIL.equalsIgnoreCase(mode)){
                submailSmsUtil.sendSms(smsMessage.getPhone(), JSON.toJSONString(smsMap));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
